// pages/outvisit/approval/outvisitStaff.js
var api = require("../../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 选中的
    selectedHidden: true,
    selectedItem: {},
    // 几个结果
    animationHidden: false,
    datalists: [],
    dataArray: {
      "id": 0,
      "name": "外访工作组",
      "num": 0,
      "children": []
    },
    scrollTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取外访组
    that.getOutGroup();
  },

  // 获取外访组
  getOutGroup: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });//显示加载中
    api.getOutGroup().then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var dataArray = that.data.dataArray;
        dataArray.children = res.data;
        this.setData({
          datalists: res.data,
          dataArray: dataArray
        });
      }
    });
  },

  // 选择一列
  selectList: function (e) {
    var children = e.currentTarget.dataset.item.children;
    if (!children) {
      children = [];
    }

    this.setData({
      selectedHidden: false,
      selectedItem: e.currentTarget.dataset.item,
      datalists: children,
      animationHidden: true,
      scrollTop: 0
    });

    this.setData({
      animationHidden: false,
      scrollTop: 48
    });
  },

  // 列表返回上一级
  backTap: function () {
    let id = this.data.selectedItem.id;
    let tempData = this.findParent(this.data.dataArray, id);
    if (tempData.id == 0) {
      this.setData({
        scrollTop: 0,
        selectedHidden: true,
        datalists: this.data.dataArray.children,
        selectedItem: {}
      })
    } else {
      this.setData({
        datalists: tempData.children,
        selectedItem: tempData
      })
    }
  },

  //递归找父级
  findParent: function (data, id) {
    if (data.children) {
      for (let i = 0; i < data.children.length; i++) {
        if (data.children[i].id == id) {
          return data;
        } else {
          let childrenData = this.findParent(data.children[i], id);
          if (childrenData != null) {
            return childrenData;
          }
        }
      }
    }
    return null;
  },

  // 选择外访人员
  selectStaff: function () {
    var that = this;
    var selectedItem = that.data.selectedItem;
    if (JSON.stringify(selectedItem) == "{}") {
      wx.showToast({
        title: "请选择",
        icon: "none"
      });
      return;
    }

    var outName = [];//外访名字
    var outIds = [];//外访id
    if (selectedItem.isParent) {
      var children = selectedItem.children;
      if (children.length == 0) {
        wx.showToast({
          title: "所选小组中的人员不能为空",
          icon: "none"
        });
        return;
      } 
      if (children[0].isParent) {
        wx.showToast({
          title: "请选择小组或人员",
          icon: "none"
        });
        return;
      }
      for (var i = 0; i < children.length; i++) {
        outName.push(children[i].name);
        outIds.push(children[i].id);
      }
    } else {
      outName.push(selectedItem.name);
      outIds.push(selectedItem.id);
    }
    outName = outName.join(",");
    
    var pages = getCurrentPages();//获取页面栈
    var prePage = pages[pages.length - 2];//上一个页面实例对象
    prePage.setData({
      outName: outName,
      outIds: outIds
    });
    wx.navigateBack({
      delta: 1
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})